// pages/home/home.js
const qcloud = require("../../vendor/wafer2-client-sdk/index.js")  // 引入wafer2自带的爬虫(网络请求)函数
const config = require("../../config.js")  // 引入API地址

Page({
  data: {
    hotMovie: {},
    hotComment: {}
  },
  onPullDownRefresh: function () {
    // 下拉刷新推荐
    this.getHot(() => {
      wx.showToast({
        title: '刷新成功',
      })
      wx.stopPullDownRefresh()
    })
  },
  /**
   * 主页载入，获取推荐
   */
  onLoad: function (options) {
    this.getHot()
  },
  getHot(callback) {
    // 获取推荐
    qcloud.request({
      url: config.service.hotCommentUrl,
      success: res => {
        let data = res.data

        if (!data.code) {
          let hotData = data.data
          console.log("推荐影评获取成功！！！")
          this.setData({
            hotMovie: hotData.hotMovie,
            hotComment: hotData.hotComment
          })
        }
        callback && callback()
      },
      fail: res => {

      }
    })
  },
  onTapToList() {
    // 跳转至电影列表
    wx.navigateTo({
      url: '../list/list',
    })
  },
  onTapToUser() {
    // 跳转至个人中心
    wx.navigateTo({
      url: '../user/user',
    })
  },
  onTapToThisMovie() {
    // 点击首页大图，跳转该电影介绍
    let movieDetail = this.data.hotMovie
    wx.navigateTo({
      url: `../detail/detail?id=${movieDetail.id}&title=${movieDetail.title}&image=${movieDetail.image}&description=${movieDetail.description}&type=${movieDetail.category}`,
    })
  },
  onTapToThisComment() {
    // 点击推荐词条，跳转该影评详情
    let data = this.data
    let movieDetail = data.hotMovie
    let id = data.hotComment.id
    wx.navigateTo({
      url: `../commentDetail/commentDetail?movie_id=${movieDetail.id}&id=${id}&title=${movieDetail.title}&image=${movieDetail.image}`,
    })
  }
})